package main

import "fmt"

/* 类型断言
   存在两种方式
   1、 x.(T)
   2、仅在switch case中使用
     x.(type)
*/

func justifyType(x interface{}) {
	switch v := x.(type) {
	case string:
		fmt.Printf("x is a string,value is %v\n", v)
	case int:
		fmt.Printf("x is a int,value is %v\n", v)
	case bool:
		fmt.Printf("x is a bool,value is %v\n", v)
	default:
		fmt.Println("UnSupport type")
	}
}

func justifyType2(x interface{}) {
	if _, ok := x.(string); ok {
		fmt.Println("x is a string")
	} else if _, ok := x.(int); ok {
		fmt.Println("x is a int")
	} else if _, ok := x.(bool); ok {
		fmt.Println("x is a bool")
	}
}

func main() {
	var a interface{}
	a = "你好,golang"

	v, ok := a.(string)
	fmt.Println(v, ok) //你好,golang true
	v1, ok := a.(int)
	fmt.Println(v1, ok) //0 false

	justifyType(false)
}
